package com.senjoeson.socialsecure.utils;

import android.content.Context;
import android.media.AudioManager;
import android.util.Log;
import android.widget.Toast;

import com.baidu.tts.auth.AuthInfo;
import com.baidu.tts.chainofresponsibility.logger.LoggerProxy;
import com.baidu.tts.client.SpeechError;
import com.baidu.tts.client.SpeechSynthesizer;
import com.baidu.tts.client.SpeechSynthesizerListener;
import com.baidu.tts.client.TtsMode;

/**
 * 语音合成工具类
 */
public class SpeechUtil implements SpeechSynthesizerListener {


    private SpeechSynthesizer mSpeechSynthesizer;
    private Context context;

    public static final String TAG = "SpeechUtil";

    public SpeechUtil(Context context) {
        this.context = context;
        init(context);
    }

    private TtsMode ttsMode = TtsMode.ONLINE;

    /**
     * 初始化合成相关组件
     */
    private void init(Context context) {
        mSpeechSynthesizer = SpeechSynthesizer.getInstance();
        // 此处需要将setApiKey方法的两个参数替换为你在百度开发者中心注册应用所得到的apiKey和secretKey
        mSpeechSynthesizer.setAppId("14219419");
        mSpeechSynthesizer.setApiKey("5WED5nCANDSyOjblg2hd9t4R", "4ip33Z5DxEA7kWN5SHtxeGyRj1p1TFpp");
        mSpeechSynthesizer.setContext(context);
        mSpeechSynthesizer.auth(TtsMode.ONLINE);
        LoggerProxy.printable(true);
        mSpeechSynthesizer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        AuthInfo authInfo = mSpeechSynthesizer.auth(ttsMode);

        if (!authInfo.isSuccess()) {
            String errorMsg = authInfo.getTtsError().getDetailMessage();
            Toast.makeText(this.context, "语音SDK认证失败：" + errorMsg, Toast.LENGTH_SHORT).show();
            return;
        }
        setParams();
       // mSpeechSynthesizer.setAudioStreamType(AudioManager.MODE_IN_CALL);
    }

    /**
     * 开始文本合成并朗读
     */
    public void speak(final String content) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                int ret = mSpeechSynthesizer.speak(content);
                if (ret != 0) {
                    Log.e("inf", "开始合成器失败：" + ret);
                }
            }
        }).start();
    }

    /**
     * 取消本次合成并停止朗读
     */
    public void stop() {
        mSpeechSynthesizer.stop();
    }


    public void release() {
        mSpeechSynthesizer.release();
    }

    /**
     * 暂停文本朗读，如果没有调用speak(String)方法或者合成器初始化失败，该方法将无任何效果
     */
    public void pause() {
        mSpeechSynthesizer.pause();
    }

    /**
     * 继续文本朗读，如果没有调用speak(String)方法或者合成器初始化失败，该方法将无任何效果
     */
    public void resume() {
        mSpeechSynthesizer.resume();
    }

    /**
     * 为语音合成器设置相关参数
     */
    private void setParams() {
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEAKER, "0");//发音人，目前支持女声(0)和男声(1)
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_VOLUME, "9");//音量，取值范围[0, 9]，数值越大，音量越大
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_SPEED, "5");//朗读语速，取值范围[0, 9]，数值越大，语速越快
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_PITCH, "5");//音调，取值范围[0, 9]，数值越大，音量越高
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_ENCODE,
                SpeechSynthesizer.AUDIO_ENCODE_AMR);//音频格式，支持bv/amr/opus/mp3，取值详见随后常量声明
        mSpeechSynthesizer.setParam(SpeechSynthesizer.PARAM_AUDIO_RATE,
                SpeechSynthesizer.AUDIO_BITRATE_AMR_15K85);//音频比特率，各音频格式支持的比特率详见随后常量声明
        int initTts = mSpeechSynthesizer.initTts(ttsMode);
        if (initTts == 0) {
            Log.d(TAG, "setParams: ");
        }
    }

    @Override
    public void onSynthesizeStart(String s) {
        Log.d(TAG, "onSynthesizeStart: 朗读开始" + s);
    }

    @Override
    public void onSynthesizeDataArrived(String s, byte[] bytes, int i) {
        Log.d(TAG, "onSynthesizeDataArrived: " + s);
    }

    @Override
    public void onSynthesizeFinish(String s) {
        Log.d(TAG, "onSynthesizeFinish: " + s);
    }

    @Override
    public void onSpeechStart(String s) {
        Log.d(TAG, "onSpeechStart: " + s);
    }

    @Override
    public void onSpeechProgressChanged(String s, int i) {
        Log.d(TAG, "onSpeechProgressChanged: " + s);
    }

    @Override
    public void onSpeechFinish(String s) {
        Log.d(TAG, "onSpeechFinish: " + s);
    }

    @Override
    public void onError(String s, SpeechError speechError) {
        Log.d(TAG, "onError: " + speechError.description);
    }
}